﻿#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <iostream>
#include <stdio.h>
#include <string>
#include <assert.h>

using namespace std;
namespace Ysz
{

	template<class T>

	class vector

	{

	public:

		// Vector的迭代器是一个原生指针

		typedef T* iterator;

		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;

		}

		const_iterator end() const
		{
			return _finish;

		}
		const_iterator cbegin() const
		{
			return _start;

		}

		const_iterator cend() const
		{
			return _finish;

		}



		// construct and destroy

		vector()
			:_start(nullptr)
			,_finish(nullptr)
			,_endOfStorage(nullptr)
		{

		}

		vector(int n, const T& value = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(value);
			}
		}
		vector(size_t n, const T& value = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(value);
			}
		}

		template<class InputIterator>

		vector(InputIterator first, InputIterator last)
		{
			reserve(last - first);
			
			while(first != last)
			{
				push_back(*first);
				first++;
			}
		}

		vector(const vector<T>& v)
		{
			reserve(v.size());
			//size_t len = v.size();

			for (const auto& x : v)
			{
				push_back(x);
			}
			/*for (size_t i = 0; i < len; i++)
			{
				_start[i] = v._start[i];
			}
			_finish += len;*/
		}

		vector<T>& operator= (vector<T> v)
		{
			swap(v);
			return *this;
		}

		~vector()
		{
			delete[] _start;
		}

			// capacity

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _endOfStorage - _start;
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t oldsize = size();
				T* tmp = new T[n];
				for (size_t i = 0; i < oldsize; i++)
				{
					tmp[i] = _start[i];
				}
				delete[] _start;
				_start = tmp;
				_finish = tmp + oldsize;
				_endOfStorage = tmp + n;
				
				
			}
		}

		void resize(size_t n, const T& value = T())
		{
			if (n > size())
			{
				reserve(n);
				n = n - size();
				for (size_t i = 0; i < n; i++)
				{
					push_back(value);
				}
			}
			else if (n < size())
			{
				_finish -= size() - n;
			}
		}

			

		///////////////access///////////////////////////////

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

		const T& operator[](size_t pos)const
		{
			assert(pos < size());
			return _start[pos];
		}



			///////////////modify/////////////////////////////

		void push_back(const T& x)
		{
			if (_finish == _endOfStorage)
			{
				size_t n = capacity() == 0 ? 4 : 2 * capacity();
				reserve(n);
			}
			_start[size()] = x;
			_finish++;
		}

		void pop_back()
		{
			assert(size());
			_finish--;
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endOfStorage, v._endOfStorage);
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos <= _finish);
			if (pos == _finish)
			{
				push_back(x);
			}
			else
			{
				size_t oldsize = pos - _start; //记录插入位置防止迭代器失效
				if (_finish == _endOfStorage)
				{
					size_t n = capacity() == 0 ? 4 : 2 * capacity();
					reserve(n);
					pos = _start + oldsize;

				}
				iterator tmp = _finish;
				while (tmp != pos)
				{
					*tmp = *(tmp - 1);
					tmp--;
				}
				*tmp = x;
				_finish++;
			}
			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(size() && pos < _finish && pos >= _start);
			iterator tmp = pos;
			while (tmp != _finish - 1)
			{
				*tmp = *(tmp + 1);
				tmp++;
			}
			_finish--;
			return pos;
		}

	private:

		iterator _start = nullptr; // 指向数据块的开始

		iterator _finish = nullptr; // 指向有效数据的尾

		iterator _endOfStorage = nullptr; // 指向存储容量的尾

	};

}
